﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Mimoza.GUI;

namespace Mimoza.PuttyPlugin
{
    public partial class DisplayPage : Mimoza.GUI.WizardPage
    {
        private PuttyData m_data;
        public DisplayPage()
        {
            InitializeComponent();
        }

        override public void Init()
        {
            m_data = this.Wizard.UserData as PuttyData;

            textBoxColumns.Text = m_data.WindowSize.Columns.ToString();
            textBoxRows.Text = m_data.WindowSize.Rows.ToString();

            textBoxLinesScroll.Text = m_data.Scrollback.ScrollbarLines.ToString();
            checkBoxShowScroll.Checked = m_data.Scrollback.IsShowScrollbarFullScreen;

            this.textBoxRows.TextChanged += new System.EventHandler(this.textBoxRows_TextChanged);
            this.textBoxColumns.TextChanged += new System.EventHandler(this.textBoxColumns_TextChanged);
            this.checkBoxShowScroll.CheckedChanged += new System.EventHandler(this.checkBoxShowScroll_CheckedChanged);
            this.textBoxLinesScroll.TextChanged += new System.EventHandler(this.textBoxLinesScroll_TextChanged);
        }

        override public bool Try2LeavePage(WizardPageLeavingDirection direction)
        {
            if (direction == WizardPageLeavingDirection.Back)
            {
                return true;
            }

            if (!CheckWindowSize()) return false;
            if (!CheckScrollbLines()) return false;

            m_data.WindowSize.Columns = int.Parse(textBoxColumns.Text);
            m_data.WindowSize.Rows = int.Parse(textBoxRows.Text);

            m_data.Scrollback.ScrollbarLines = int.Parse(textBoxLinesScroll.Text);
            m_data.Scrollback.IsShowScrollbarFullScreen = checkBoxShowScroll.Checked;

            return true;
        }

        bool CheckWindowSize()
        {
            if (!CheckIntegerString(textBoxColumns) || !CheckIntegerString(textBoxRows))
            {
                MessageBox.Show(Resource.WrongWindowSize, Resource.WrongParametersCaption,
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            return true;
        }

        bool CheckScrollbLines()
        {
            if (!CheckIntegerString(textBoxLinesScroll))
            {
                MessageBox.Show(Resource.WrongScrollbackLines, Resource.WrongParametersCaption,
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            return true;
        }

        static bool CheckIntegerString(TextBox textBox)
        {
            string strVal = textBox.Text;
            int val = 0;
            if (string.IsNullOrEmpty(strVal) || !int.TryParse(strVal, out val) || val < 0)
            {
                textBox.Focus();
                return false;
            }
            else return true;
        }

        private void textBoxColumns_TextChanged(object sender, EventArgs e)
        {
            this.RaiseOnPageModified();
        }

        private void textBoxRows_TextChanged(object sender, EventArgs e)
        {
            this.RaiseOnPageModified();
        }

        private void textBoxLinesScroll_TextChanged(object sender, EventArgs e)
        {
            this.RaiseOnPageModified();
        }

        private void checkBoxShowScroll_CheckedChanged(object sender, EventArgs e)
        {
            this.RaiseOnPageModified();
        }
    }
}
